package com.example.homecunter.news;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.common.utils.BaseEntity;
import com.example.homecunter.BR;
import com.example.homecunter.R;
import com.example.homecunter.news.adapter.NewsPagerAdapter;
import com.example.homecunter.news.newsentity.NewsType;
import com.example.homecunter.news.newsviewmodel.NewsViewModel;
import com.example.mvvmcore.fragment.BaseMVVMFragment;
import com.google.android.material.tabs.TabLayout;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

public class HomeFragment extends BaseMVVMFragment<NewsViewModel> {
    View view;
    private EditText etSeek;
    private TabLayout homeTab;
    private ViewPager homeVp;
    private ImageView img;
    private ImageView homeImg;
    private TextView homeTvGx;
    private ImageView imgFx;

    @Override
    protected void initEnv() {
    }

    @Override
    protected void initData() {
        etSeek = (EditText) view.findViewById(R.id.et_seek);
        homeTab = (TabLayout) view.findViewById(R.id.home_tab);
        homeVp = (ViewPager) view.findViewById(R.id.home_vp);
        homeImg = (ImageView) view.findViewById(R.id.home_img);
        imgFx = (ImageView) view.findViewById(R.id.img_fx);

        homeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建对象
                PopupWindow popupWindow = new PopupWindow(getContext());
                //设置属性  三大要素
                popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                //布局样式
                popupWindow.setContentView(LayoutInflater.from(getContext()).inflate(R.layout.popupwindow, null));
                //点击外部关闭
                popupWindow.setOutsideTouchable(true);

                //显示窗体
//                View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_home, null);
//                popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
                popupWindow.showAsDropDown(homeTab, 0, 0);
            }
        });


        etSeek.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/home/search")
                        .navigation();
            }
        });

        imgFx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    protected Class getViewModelClazz() {
        return NewsViewModel.class;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void preparVarMap(Map<Integer, Object> map) {
        map.put(BR.fraghome, this);
    }

    @Override
    protected void stopLoad() {

    }

    @Override
    protected void lazyLoad() {
        Log.i("--b", "lazyLoad: HomeFragment");


        mViewModel.getTypt().observe(getActivity(), new Observer<BaseEntity<List<NewsType>>>() {
            @Override
            public void onChanged(BaseEntity<List<NewsType>> listBaseEntity) {
                List<NewsType> data1 = listBaseEntity.getData();
                NewsPagerAdapter newsPagerAdapter = new NewsPagerAdapter(getActivity().getSupportFragmentManager(), data1);

                homeVp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(homeTab));
                homeVp.setAdapter(newsPagerAdapter);
                homeTab.setupWithViewPager(homeVp);


            }
        });


        /**
         * tablayout 点击放大
         */
        homeTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

//        homeTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                changeTabSelect(tab);
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//                changeTabNormal(tab);
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
    }

//    private void changeTabSelect(TabLayout.Tab tab) {
//        final View view = tab.getCustomView();
//        ObjectAnimator anim = ObjectAnimator
//                .ofFloat(view,"", 1.0F, 1.1F)
//                .setDuration(200);
//        anim.start();
//        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float cVal = (Float) animation.getAnimatedValue();
//                view.setAlpha(0.5f + (cVal - 1f) * (0.5f / 0.1f));
//                view.setScaleX(cVal);
//                view.setScaleY(cVal);
//            }
//        });
//    }
//
//    /**
//     * 改变TabLayout的View到未选中状态
//     */
//    private void changeTabNormal(TabLayout.Tab tab) {
//        final View view = tab.getCustomView();
//        ObjectAnimator anim = ObjectAnimator
//                .ofFloat(view,"", 1.0F, 0.9F)
//                .setDuration(200);
//        anim.start();
//        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float cVal = (Float) animation.getAnimatedValue();
//                view.setAlpha(1f - (1f - cVal) * (0.5f / 0.1f));
//                view.setScaleX(cVal);
//                view.setScaleY(cVal);
//            }
//        });
//    }


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }


}